#!/bin/sh
if [ "$1" = "--help" ]; then
    echo "Description: Automation script to handle the build and distribution process of this project"
    echo "Options:"
    echo "$0 --generate"
    echo "  generates configuration aclocal.m4 and Makefile.in if any are missing"
    echo "$0 --regenerate"
    echo "  generates configuration aclocal.m4 and Makefile.in even if they already exist"
    echo "$0 --configure [--enable-tests] [--enable-opencl]"
    echo "  runs configuration if Makefile is not found, option to include tests"
    echo "$0 --reconfigure [--enable-tests] [--enable-opencl]"
    echo "  force runs configuration, option to include tests"
    echo "$0 --test"
    echo "  build and run the test suite"
    echo "$0 --build"
    echo "  build the program"
    echo "$0 --distribute"
    echo "  build the tarball"
    echo "$0 --install"
    echo "  build and install the program"
    echo "$0 --install-python"
    echo "  build and install the python extension"
elif [ "$1" = "--clean" ]; then
    echo "Cleaning Dependency Tests"
    if [ -f "Makefile" -a -f "config.status" ]; then
        make clean
    fi
    for file in config.log autom4te.cache aclocal.m4; do
        if [ -f $file -o -d $file ]; then
            echo "removing $file"
            rm -rf $file
        fi
    done
elif [ "$1" = "--generate" ]; then
    if [ ! -f "configure" -o ! -f "Makefile.in" -o "$2" = "--force" ]; then
        echo "Generating Dependency Tests" 
        autoreconf --install --warnings=all && aclocal && automake || echo "Failed to generate dependency tests."
        ./$0 --clean
    fi
elif [ "$1" = "--regenerate" ]; then
    ./$0 --generate --force
elif [ "$1" = "--configure" ]; then
    ./$0 --generate
    if [ ! -f "Makefile" ]; then
        echo "Executing Dependency Tests"
        ./configure $2
        ./$0 --clean
    fi
elif [ "$1" = "--reconfigure" ]; then
    ./$0 --generate
    echo "Executing Dependency Tests"
    ./configure $2
    ./$0 --clean
elif [ "$1" = "--test" ]; then
    ./$0 --configure --enable-tests && make -j4 test
elif [ "$1" = "--build" ]; then
    ./$0 --configure $2 $3 && make -j4 gosdt && mv gosdt build/
elif [ "$1" = "--distribute" ]; then
    ./$0 --configure $2 $3 && make -j4 dist && mv gosdt-* build/
elif [ "$1" = "--install" ]; then
    ./$0 --configure $2 $3 && make -j4 && make install
elif [ "$1" = "--install-python" ]; then
    ./$0 --configure $2 $3 && make -j4 clean && python3 setup.py clean && python3 setup.py build && python3 setup.py install
else
    ./$0 --help
fi